/***************************************************
  > Copyright (C) 2024 ==KINGYI== All rights reserved.
  > File Name: client.c
  > Author:
  > Mail: 
  > Created Time: 2024年09月20日 星期五 16时33分31秒
 ***************************************************/
#include "client.h"
int main(int argc,const char* argv[])
{
	char principal[20] = {0};
	Login log;
	int num = 0;
	while(1)
	{

		printf("安全员登录系统\n");
		printf("输入1注册\n");
		printf("输入2登录\n");
		printf("输入0退出\n");
		scanf("%d",&num);
		int sockfd = create_addr();
		switch(num)
		{
			case 1 :
				enroll(sockfd);
				break;
			case 2 :
				regist(sockfd);
				break;		
			case 0 :
				return 0;
		}
		strcpy(principal,log.account);	
		gather(principal,sockfd);

	}
	return 0;
}

void gather(char *principal,int sockfd)
{
	cJSON *json =NULL;

	int ret = 0;
	char dest[1024] = {0};
	Info fp;
	time_t now;
	time(&now);
	struct tm *lm;
	lm = localtime(&now);

	char id[10] = {"safe"};
	while(1)
	{

		memset(&fp,0,sizeof(Info));
		fp.type = 3;
		printf("采集几号采集点输入");
		scanf("%s",fp.pt);
		printf("输入温度");
		scanf("%f",&fp.temp);

		printf("输入湿度");
		scanf("%f",&fp.humidity);

		printf("输入氧气浓度");
		scanf("%f",&fp.O2);

		printf("输入二氧化碳");
		scanf("%f",&fp.CO2);


		printf("输入甲烷浓度");
		scanf("%f",&fp.CH4);

		printf("输入一氧化碳");
		scanf("%f",&fp.CO);

		sprintf(fp.time,"years:%d mon:%d day:%d hour:%d min:%d sec:%d",\
				lm->tm_year + 1900, lm->tm_mon + 1, lm->tm_mday,lm->tm_hour, lm->tm_min, lm->tm_sec);

		strcpy(fp.principal,principal);

		json = cJSON_CreateObject();
		cJSON_AddItemToObject(json,"id",cJSON_CreateString(id));
		cJSON_AddItemToObject(json,"time",cJSON_CreateString(fp.time));			
		cJSON_AddItemToObject(json,"pt",cJSON_CreateString(fp.pt));
		cJSON_AddItemToObject(json,"principal",cJSON_CreateString(fp.principal));
		cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(fp.type));
		cJSON_AddItemToObject(json,"temp",cJSON_CreateNumber(fp.temp));
		cJSON_AddItemToObject(json,"humidity",cJSON_CreateNumber(fp.humidity));
		cJSON_AddItemToObject(json,"O2",cJSON_CreateNumber(fp.O2));
		cJSON_AddItemToObject(json,"CO2",cJSON_CreateNumber(fp.CO2));
		cJSON_AddItemToObject(json,"CH4",cJSON_CreateNumber(fp.CH4));
		cJSON_AddItemToObject(json,"CO",cJSON_CreateNumber(fp.CO));


		char *buf = cJSON_Print(json);

		ret = send(sockfd,buf,strlen(buf),0);
		if(ret == -1)
		{
			perror("send");
			exit(-1);
		}

		ret = recv(sockfd,dest,sizeof(dest),0);
		if(ret == -1)
		{
			perror("recv");
			exit(-1);
		}

		cJSON *responseJson = cJSON_Parse(buf);

		cJSON *node = cJSON_GetObjectItem(responseJson, "res");
		if(node == NULL)
		{
			printf("can't find\n");
			exit(-1);
		}
		if(node->type == cJSON_String)
		{
			strcpy(fp.res,node->valuestring); 
		}
		node = cJSON_GetObjectItem(responseJson, "state");
		if(node == NULL)
		{
			printf("can't find\n");
			exit(-1);
		}
		if(node->type == cJSON_String)
		{
			strcpy(fp.state,node->valuestring); 
		}

		printf("%s",fp.res);
		if(strcmp(fp.state,"anomaly"))
		{
			printf("risk,evacuate");
		}
	}
	cJSON_Delete(json);				
	close(sockfd);

}




